// 2013-7-31下午01:18:24  net.betou.trade.role.search.setting.CURDConfig.java

package net.betou.trade.role.setting;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.entity.main.RoleExt;

/**
 * 更新、删除、读取配置 
 * @author 叶装装
 */
public abstract class URDConfig{
	
	public static enum Type{
		SELF,  FILTER
	}
	
	public static enum Operating{
		UPDATE, DELETE, FIND
	}
	
	protected abstract String getBegin( );
	protected abstract boolean findSelf( );
	protected abstract String getEnd( );
	
	public URDConfig( ){}
	
	private AgentAndAdmins admin;
	private RoleExt roleExt;
	protected Type type = Type.SELF;
	
	private String config;
	private Integer deep;
	
	public URDConfig( AgentAndAdmins admin, String config ){
		this.admin = admin;
		this.config = config;
		this.initRoleExt( );
	}
	
	public Boolean isSelf( ){
		return this.type == Type.SELF;
	}
	
	// 前端调用此方法时，已经经过了判断，所以此处可以不用判断
	public String getDeepSQL(  ){
		Integer adminId = admin.getId( );
		String viewRoles = getViewRoles( );
		return getBegin( ) + this.getSQL( deep, adminId, viewRoles ) + getEnd( );
	}
	
	private String getSQL( Integer deep, Integer adminId, String viewRoles ){
		StringBuffer sqlBuffer = new StringBuffer( );
		sqlBuffer.append( "select t.admin_id from trade_admin_role t, trade_admin a where t.admin_id = a.id and a.deep <=" + deep );
		sqlBuffer.append( " and find_in_set(" + adminId + ",a.top_intermedia) " );
		sqlBuffer.append( " and " + viewRoles );
		
		if( findSelf ( ) ){
			sqlBuffer.append( " or t.admin_id=" + adminId );
		}
		
		sqlBuffer.append( "  group by t.admin_id" );
		
		return sqlBuffer.toString( );
	}
	
	
	private String getViewRoles( ){
		String viewRoles = roleExt.getViewRoles( );
		
		String [ ] splitViewRoles = StringUtils.split( viewRoles, "," );
		
		if( splitViewRoles == null || splitViewRoles.length == 0 ){
			return null;
		}
		
		StringBuffer buffer = new StringBuffer( "(");
		
		for( int i = 0; i < splitViewRoles.length; i++ ){
			String id = splitViewRoles[ i ];
			
			buffer.append( "t.role_id=" );
			buffer.append( id );
			
			if( i < splitViewRoles.length - 1 ){
				buffer.append( " or " );
			}
		}
		
		buffer.append( ")" );
		
		return buffer.toString( );
	}
	
	/**
	 * 初始化 roleExt 
	 */
	protected void initRoleExt( ){
		Role role = admin.getSearchSettingRole( ); // 获取角色
		if( role == null ) {
			return;
		}
		
		Map< String, RoleExt > extMap = role.getSearchSettingRoleExt( );
		
		if( extMap == null ){
			return;
		}
		roleExt = extMap.get( config ); // 获取该配置
		
		if( roleExt == null ){
			return;
		}
		
		this.deep = getDeep( );
	}
	
	private int getDeep( ){
		Operating op = operating( );
		Integer deep; // 如果deep为0,就表示还是自己的
		
		if( op == Operating.UPDATE ){ // 默认为findDeep
			deep = roleExt.getUpdateDeep( );
		}else if( op == Operating.DELETE ){
			deep = roleExt.getDeleteDeep( );
		}else {
			deep = roleExt.getFindDeep( );
		}
		
		if( deep == null ){
			deep = 0 ; // 进行一次空值的处理， 不过这种情况基本上不会出现
		}
		
		if( deep == 0 ){
			return deep; // 不进行判断，直接返回
		}
		
		String viewRoles = roleExt.getViewRoles( ); // 如果没有，只会为空
		if( StringUtils.isNotBlank( viewRoles ) && deep > 0){
			this.type = Type.FILTER;
		}
		
		return deep;
	}
	
	protected abstract Operating operating( );
	
	
}
